package com.orderMeal.common.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport
{
	private static final long serialVersionUID = 1L;
	protected boolean isNull(Object NULL)
	{
		if (NULL == null || NULL.toString().trim().length() == 0)
		{
			return true;
		}
		return false;
	}
	protected void setSessionInvalidate()
	{
		ServletActionContext.getRequest().getSession().invalidate();
	}
	protected boolean isExistSessionAttriubte(String key)
	{
		if (ActionContext.getContext().getSession().get(key) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	protected void setApplicationAttribute(String key, Object obj)
	{
		ActionContext.getContext().getApplication().put(key, obj);
	}
	protected Object getApplicationAttribute(String key)
	{
		return ActionContext.getContext().getApplication().get(key);
	}
	@SuppressWarnings("unchecked")
	protected void setSessionAttribute(String key, Object obj)
	{
		ActionContext.getContext().getSession().put(key, obj);
	}
	protected Object getSessionAttribute(String key)
	{
		return ActionContext.getContext().getSession().get(key);
	}
	protected Object removeSessionAttribute(String key)
	{
		return ActionContext.getContext().getSession().remove(key);
	}
	protected String getScheme()
	{
		return ServletActionContext.getRequest().getScheme();
	}
	protected String getRemoteAddr()
	{
		return ServletActionContext.getRequest().getRemoteAddr();
	}
	protected String getServerPort()
	{
		return "" + ServletActionContext.getRequest().getServerPort();
	}
	protected String getContextPath()
	{
		return "" + ServletActionContext.getRequest().getContextPath();
	}
	protected String getServletPath()
	{
		return ServletActionContext.getRequest().getServletPath();
	}

 
	protected String getQueryString()
	{
		return ServletActionContext.getRequest().getQueryString();
	}
	protected String getAccessMethod()
	{
		return ServletActionContext.getRequest().getMethod();
	}
	protected String getServletURL()
	{
		return getScheme() + "://" + getRemoteAddr() + ":" + getServerPort() + getContextPath();
	}
	protected String getFullURL()
	{
		return getScheme() + "://" + getRemoteAddr() + ":" + getServerPort() + getContextPath() + getServletPath() + "?" + getQueryString();
	}
	protected String getRequestParameter(String key)
	{
		return ServletActionContext.getRequest().getParameter(key);
	}
	protected Map getRequestParameterMap()
	{
		return ServletActionContext.getRequest().getParameterMap();
	}
	protected void setRequestAttribute(String key, Object obj)
	{
		ServletActionContext.getRequest().setAttribute(key, obj);
	}

	protected Object getRequestAttribute(String key)
	{
		return ServletActionContext.getRequest().getAttribute(key);
	}
	protected void setContextAttribute(String key, Object obj)
	{
		ActionContext.getContext().put(key, obj);
	}
	protected Object getContextAttribute(String key)
	{
		return ActionContext.getContext().get(key);
	}
	protected ActionContext getActionContext()
	{
		return ActionContext.getContext();
	}
	protected void saveActionError(String key) {
		super.addActionError(super.getText(key));
	}
	protected void saveActionMessage(String key) {
		super.addActionMessage(super.getText(key));
	}
	protected String getRequestPath() {
		return (String)ServletActionContext.getRequest().getServletPath();
	}
	protected void copyBeanProperties(Object targer, Object source) {
		if (source != null) {
			try {
				BeanUtils.copyProperties(targer, source);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	protected void renderText(String text) {
		try {
			ServletActionContext.getResponse().setContentType("text/plain;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void renderText(String text, String charset) {
		try {
			ServletActionContext.getResponse().setContentType("text/plain;charset="+charset);
			ServletActionContext.getResponse().getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void renderHtml(String text) {
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.write(text);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void renderHtml(String text, String charset) {
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset="+charset);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.write(text);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void renderXML(String text) {
		try {
			ServletActionContext.getResponse().setContentType("text/xml;charset=UTF-8");
			ServletActionContext.getResponse().getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void renderXML(String text, String charset) {
		try {
			ServletActionContext.getResponse().setContentType("text/xml;charset="+charset);
			ServletActionContext.getResponse().getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
